package com.huo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huo.constant.MessageConstant;
import com.huo.constant.RedisConstant;
import com.huo.entity.PageResult;
import com.huo.entity.QueryPageBean;
import com.huo.entity.Result;
import com.huo.pojo.Setmeal;
import com.huo.service.SetmealService;
import com.huo.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * @Description: 套餐控制层
 * @Author: 汪青松
 * @CreateDate: 2021/1/29 21:44
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    /**
     * @Description: 上传文件
     * @Author: 汪青松
     * @Param: [imgFile]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/22 16:17
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获得文件后缀
            String suffix = originalFilename.substring(lastIndexOf);
            String imageName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), imageName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, imageName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, imageName);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * @Description: 添加套餐
     * @Author: 汪青松
     * @Param: [checkgroupIds, setmeal]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/22 17:02
     */
    @PostMapping("/add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        try {
            setmealService.add(checkgroupIds, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * @Description: 分页
     * @Author: 汪青松
     * @Param: [queryPageBean]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/24 18:13
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findPage(queryPageBean);
    }
}

